﻿using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;

namespace CommonManagement
{
    /// <summary>
    /// 余额变动应用服务类
    /// </summary>
    public class BalanceAppService : ApplicationService, IBalanceAppService
    {
        private readonly IAuthManager _authManager;
        private readonly BalanceManager _balanceManager;
        private readonly RechargeManager _rechargeManager;
        private readonly IRepository<User, int> _userRepository;
        private readonly IRepository<Balance, int> _balanceRepository;

        public BalanceAppService(
            IAuthManager authManager,
            BalanceManager balanceManager,
            RechargeManager rechargeManager,
            IRepository<User, int> userRepository,
            IRepository<Balance, int> balanceRepository)
        {
            _authManager = authManager;
            _balanceManager = balanceManager;
            _rechargeManager = rechargeManager;
            _userRepository = userRepository;
            _balanceRepository = balanceRepository;
        }

        // 获取分页列表
        public async Task<ListResponse<BalanceDto>> PagedListAsync(PagedListRequest request)
        {
            // 查询
            var userId = _authManager.GetUserId();
            var query = _balanceRepository
                .Where(m => m.UserId == userId)
                .AsQueryable();
            // 计数
            var count = await query.CountAsync();
            // 排序
            if (!string.IsNullOrWhiteSpace(request.OrderByColumn))
            {
                var orderColumns = request.OrderByColumn.Split(',');
                foreach (var column in orderColumns)
                {
                    query = query.OrderBy(column);
                }
            }
            // 分页
            query = query.Page(request.PageNum, request.PageSize);
            // 返回结果
            var list = await query.ToListAsync();
            var dtos = ObjectMapper.Map<List<Balance>, List<BalanceDto>>(list);
            return ListResponse<BalanceDto>.Successful(count, dtos);
        }

        // 充值
        [UnitOfWork]
        public async Task<ActionResponse> RechargeAsync(BalanceRechargeDto input)
        {
            // 读取用户Id
            var userId = _authManager.GetUserId();
            // 增加余额
            var user = await _userRepository.GetAsync(userId);
            user.Recharge(input.Money);
            // 余额变动记录
            await _balanceManager.CreateAsync(
                userId,
                null,
                "收入",
                "充值",
                input.Money);
            // 充值记录
            await _rechargeManager.CreateAsync(
                userId,
                input.Money,
                null);
            // 返回结果
            return ActionResponse.Successful();
        }
    }
}